package com.beehoo.dcportal.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beehoo.dcportal.admin.dao.ComplaintDao;
import com.beehoo.dcportal.admin.service.ComplaintService;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.common.base.BaseService;
import com.beehoo.dcportal.model.ComplaintBean;
import com.beehoo.dcportal.utils.CommonUtils;

public class ComplaintServiceImpl extends BaseService implements
		ComplaintService {
	
	private ComplaintDao complaintDao;

	public void setComplaintDao(ComplaintDao complaintDao) {
		this.complaintDao = complaintDao;
	}

	@Override
	public void auditComplaint(ComplaintBean complaintBean) throws BaseException {
		validateAudit(complaintBean);
		String affairId = complaintDao.queryAffairId();
		if(CommonUtils.nvlToString(affairId).equals("")){
			throw new BaseException("custom.messages.error.complaint.submit.error");
		}
		complaintBean.setAffairId(affairId);
		complaintDao.auditComplaint(complaintBean);
	}

	@Override
	public void delComplaint(String queId) throws BaseException {
		if (CommonUtils.nvlToString(queId).equals("")) {
			throw new BaseException("custom.messages.error.complaint.id.empty");
		}
		int ret = complaintDao.delComplaint(queId);
		if(ret <= 0){
			throw new BaseException("custom.messages.error.complaint.del.error");
		}
	}

	@Override
	public ComplaintBean queryComplaint(String streetId, String queId)
			throws BaseException {
		if (CommonUtils.nvlToString(streetId).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		if (CommonUtils.nvlToString(queId).equals("")) {
			throw new BaseException("custom.messages.error.complaint.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("queId", queId);
		return complaintDao.queryComplaint(paramMap);
	}

	@Override
	public List<ComplaintBean> queryComplaints(String streetId,
			String queState, String beginDate, String endDate, int pageNo,
			int pageRows)
			throws BaseException {
		if (CommonUtils.nvlToString(streetId).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("queState", queState);
		paramMap.put("beginDate", beginDate + " 00:00:00");
		paramMap.put("endDate", endDate + " 23:59:59");
		paramMap.put("offset", (pageNo-1)*pageRows);
		paramMap.put("pageRows", pageRows);
		return complaintDao.queryComplaints(paramMap);
	}
	

	@Override
	public int queryComplaintCount(String streetId, String queState,
			String beginDate, String endDate) throws BaseException {
		if (CommonUtils.nvlToString(streetId).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("queState", queState);
		paramMap.put("beginDate", beginDate + " 00:00:00");
		paramMap.put("endDate", endDate + " 23:59:59");
		return complaintDao.queryComplaintCount(paramMap);
	}
	
	private void validateAudit(ComplaintBean complaintBean) throws BaseException {
		if (complaintBean == null) {
			throw new BaseException("custom.messages.error.complaint.audit.error");
		}
		if (CommonUtils.nvlToString(complaintBean.getQueId()).equals("")) {
			throw new BaseException("custom.messages.error.complaint.id.empty");
		}
		if (CommonUtils.nvlToString(complaintBean.getAuditorId()).equals("")) {
			throw new BaseException(
					"custom.messages.error.complaint.auditor.empty");
		}
		if (CommonUtils.nvlToString(complaintBean.getAuditor()).equals("")) {
			throw new BaseException(
					"custom.messages.error.complaint.auditor.empty");
		}
	}

}
